package com.danieltoms.gcSync.util;

import com.danieltoms.gcSync.contact.BBContact;
import com.danieltoms.gcSync.google.GoogleContact;

public class NameUtil
{
	public static final String GIVEN_FAMILY = "GIVEN_FAMILY";
	public static final String FAMILY_GIVEN = "FAMILY_GIVEN";
	public static final String SPLIT_TO_GIVEN = "SPLIT_TO_GIVEN";
	public static final String SPLIT_TO_FAMILY = "SPLIT_TO_FAMILY";

	public static void test()
	{
		String nameFormat = GIVEN_FAMILY;
		String split = SPLIT_TO_GIVEN;
		String test = "Daniel Joseph Borg Toms 3rd";
		StringTokenizer tokens = new StringTokenizer(test);
		// testNameTokenizer(nameFormat, split, tokens);

		nameFormat = GIVEN_FAMILY;
		split = SPLIT_TO_FAMILY;
		tokens = new StringTokenizer(test);
		// testNameTokenizer(nameFormat, split, tokens);

		nameFormat = FAMILY_GIVEN;
		split = SPLIT_TO_FAMILY;
		tokens = new StringTokenizer(test);
		// testNameTokenizer(nameFormat, split, tokens);

		nameFormat = FAMILY_GIVEN;
		split = SPLIT_TO_GIVEN;
		tokens = new StringTokenizer(test);
		// testNameTokenizer(nameFormat, split, tokens);
	}

	public static void setGoogleName(String nameFormat, GoogleContact gc, BBContact bb)
	{
		StringBuffer name = new StringBuffer();

		if (nameFormat.equals(GIVEN_FAMILY))
		{
			name.append(bb.getGivenName());
			name.append(" ");
			name.append(bb.getFamilyName());
		}
		else if (nameFormat.equals(FAMILY_GIVEN))
		{
			name.append(bb.getFamilyName());
			name.append(" ");
			name.append(bb.getGivenName());

		}

	}

	private static void log(String msg)
	{
		System.out.println(msg);
	}

	public static void setBBName(String nameFormat, String split, BBContact bb, GoogleContact gc)
	{
		StringBuffer familyName = new StringBuffer();
		StringBuffer givenName = new StringBuffer();
		System.out.println("nameFormat: " + nameFormat);
		System.out.println("split: " + split);
		int halfway = 0;
		StringTokenizer tokens = new StringTokenizer(gc.getName());
		int numTokens = tokens.countTokens();
		if (numTokens % 2 == 0)
		{
			halfway = numTokens / 2;
		}
		else
		{
			if (nameFormat.equals(GIVEN_FAMILY) && split.equals(SPLIT_TO_GIVEN))
				halfway = (numTokens / 2) + 1;
			else if (nameFormat.equals(FAMILY_GIVEN) && split.equals(SPLIT_TO_GIVEN))
				halfway = (numTokens / 2);
			if (nameFormat.equals(GIVEN_FAMILY) && split.equals(SPLIT_TO_FAMILY))
				halfway = (numTokens / 2);
			else if (nameFormat.equals(FAMILY_GIVEN) && split.equals(SPLIT_TO_FAMILY))
				halfway = (numTokens / 2) + 1;

		}

		int i = 0;
		log("number tokens: " + numTokens);
		while (tokens.hasMoreElements())
		{
			String token = tokens.next();
			log("processing token: " + token);
			if (i < halfway)
			{
				if (nameFormat.equals(GIVEN_FAMILY))
				{
					if (givenName.length() > 0)
						givenName.append(" ");

					givenName.append(token);
				}

				else if (nameFormat.equals(FAMILY_GIVEN))
				{
					if (familyName.length() > 0)
						familyName.append(" ");

					familyName.append(token);
				}
			}
			else
			{
				if (nameFormat.equals(GIVEN_FAMILY))
				{
					if (familyName.length() > 0)
						familyName.append(" ");

					familyName.append(token);
				}
				else if (nameFormat.equals(FAMILY_GIVEN))
				{
					if (givenName.length() > 0)
						givenName.append(" ");

					givenName.append(token);

				}
			}

			i++;

		}

		bb.setGivenName(givenName.toString());
		bb.setFamilyName(familyName.toString());

	}
}
